package com.yanqu.road.dao.impl.cross.mammon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.cross.CrossUserDecoration;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossMammonDaoImpl extends TempDataObjectDao<CrossUserDecoration> {

    public void getCrossUserMammonMap(Map<Long, Map<Long, Map<Integer, CrossUserDecoration>>> allCrossUserMammonMap,
            Map<Integer, List<CrossUserDecoration>> crossGroupMammonMap) {
        String sql = "select * from t_c_mammon";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUserDecoration data = parse(rs);
                Map<Long, Map<Integer, CrossUserDecoration>> tempServerMap = allCrossUserMammonMap.get(data.getUserBaseInfo().getServerId());
                if(null == tempServerMap){
                    tempServerMap = new ConcurrentHashMap<>();
                    allCrossUserMammonMap.put(data.getUserBaseInfo().getServerId(), tempServerMap);
                }
                Map<Integer, CrossUserDecoration> tempUserMap = tempServerMap.get(data.getUserId());
                if(null == tempUserMap){
                    tempUserMap = new ConcurrentHashMap<>();
                    tempServerMap.put(data.getUserId(), tempUserMap);
                }
                tempUserMap.put(data.getDecorationId(), data);

                if(!crossGroupMammonMap.containsKey(data.getDecorationId())){
                    crossGroupMammonMap.put(data.getDecorationId(), new ArrayList<>());
                }
                crossGroupMammonMap.get(data.getDecorationId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    @Override
    protected boolean addData(CrossUserDecoration data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_mammon(serverId,userId,decorationId,endTime,userBaseInfo,modifyTime) values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserBaseInfo().getServerId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDecorationId());
            call.setLong(i++, data.getEndTime());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            call.setLong(i++, data.getModifyTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(CrossUserDecoration data) {
        PreparedStatement call = null;
        String sql = "update t_c_mammon set endTime=?,userBaseInfo=?,modifyTime=? where serverId=? and userId=? and decorationId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getEndTime());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getUserBaseInfo().getServerId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDecorationId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected CrossUserDecoration parseData(ResultSet rs) throws SQLException {
        CrossUserDecoration data = new CrossUserDecoration();
        data.setDecorationId(rs.getInt("decorationId"));
        data.setUserId(rs.getLong("userId"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setEndTime(rs.getLong("endTime"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo){
            data.setUserBaseInfo(userBaseInfo);
        }
        return data;
    }
}
